home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_dummy_thread.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  7KB  |  184 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. '''Generic thread tests.
  5.  
  6. Meant to be used by dummy_thread and thread.  To allow for different modules
  7. to be used, test_main() can be called with the module to use as the thread
  8. implementation as its sole argument.
  9.  
  10. '''
  11. import dummy_thread as _thread
  12. import time
  13. import Queue
  14. import random
  15. import unittest
  16. from test import test_support
  17. DELAY = 0
  18.  
  19. class LockTests(unittest.TestCase):
  20.     '''Test lock objects.'''
  21.     
  22.     def setUp(self):
  23.         self.lock = _thread.allocate_lock()
  24.  
  25.     
  26.     def test_initlock(self):
  27.         self.failUnless(not self.lock.locked(), 'Lock object is not initialized unlocked.')
  28.  
  29.     
  30.     def test_release(self):
  31.         self.lock.acquire()
  32.         self.lock.release()
  33.         self.failUnless(not self.lock.locked(), 'Lock object did not release properly.')
  34.  
  35.     
  36.     def test_improper_release(self):
  37.         self.failUnlessRaises(_thread.error, self.lock.release)
  38.  
  39.     
  40.     def test_cond_acquire_success(self):
  41.         self.failUnless(self.lock.acquire(0), 'Conditional acquiring of the lock failed.')
  42.  
  43.     
  44.     def test_cond_acquire_fail(self):
  45.         self.lock.acquire(0)
  46.         self.failUnless(not self.lock.acquire(0), 'Conditional acquiring of a locked lock incorrectly succeeded.')
  47.  
  48.     
  49.     def test_uncond_acquire_success(self):
  50.         self.lock.acquire()
  51.         self.failUnless(self.lock.locked(), 'Uncondional locking failed.')
  52.  
  53.     
  54.     def test_uncond_acquire_return_val(self):
  55.         self.failUnless(self.lock.acquire(1) is True, 'Unconditional locking did not return True.')
  56.  
  57.     
  58.     def test_uncond_acquire_blocking(self):
  59.         
  60.         def delay_unlock(to_unlock, delay):
  61.             '''Hold on to lock for a set amount of time before unlocking.'''
  62.             time.sleep(delay)
  63.             to_unlock.release()
  64.  
  65.         self.lock.acquire()
  66.         start_time = int(time.time())
  67.         _thread.start_new_thread(delay_unlock, (self.lock, DELAY))
  68.         if test_support.verbose:
  69.             print 
  70.             print '*** Waiting for thread to release the lock (approx. %s sec.) ***' % DELAY
  71.         
  72.         self.lock.acquire()
  73.         end_time = int(time.time())
  74.         if test_support.verbose:
  75.             print 'done'
  76.         
  77.         self.failUnless(end_time - start_time >= DELAY, 'Blocking by unconditional acquiring failed.')
  78.  
  79.  
  80.  
  81. class MiscTests(unittest.TestCase):
  82.     '''Miscellaneous tests.'''
  83.     
  84.     def test_exit(self):
  85.         self.failUnlessRaises(SystemExit, _thread.exit)
  86.  
  87.     
  88.     def test_ident(self):
  89.         self.failUnless(isinstance(_thread.get_ident(), int), '_thread.get_ident() returned a non-integer')
  90.         self.failUnless(_thread.get_ident() != 0, '_thread.get_ident() returned 0')
  91.  
  92.     
  93.     def test_LockType(self):
  94.         self.failUnless(isinstance(_thread.allocate_lock(), _thread.LockType), '_thread.LockType is not an instance of what is returned by _thread.allocate_lock()')
  95.  
  96.     
  97.     def test_interrupt_main(self):
  98.         
  99.         def call_interrupt():
  100.             _thread.interrupt_main()
  101.  
  102.         self.failUnlessRaises(KeyboardInterrupt, _thread.start_new_thread, call_interrupt, tuple())
  103.  
  104.     
  105.     def test_interrupt_in_main(self):
  106.         self.failUnlessRaises(KeyboardInterrupt, _thread.interrupt_main)
  107.  
  108.  
  109.  
  110. class ThreadTests(unittest.TestCase):
  111.     '''Test thread creation.'''
  112.     
  113.     def test_arg_passing(self):
  114.         
  115.         def arg_tester(queue, arg1 = False, arg2 = False):
  116.             '''Use to test _thread.start_new_thread() passes args properly.'''
  117.             queue.put((arg1, arg2))
  118.  
  119.         testing_queue = Queue.Queue(1)
  120.         _thread.start_new_thread(arg_tester, (testing_queue, True, True))
  121.         result = testing_queue.get()
  122.         if result[0]:
  123.             pass
  124.         self.failUnless(result[1], 'Argument passing for thread creation using tuple failed')
  125.         _thread.start_new_thread(arg_tester, tuple(), {
  126.             'queue': testing_queue,
  127.             'arg1': True,
  128.             'arg2': True })
  129.         result = testing_queue.get()
  130.         if result[0]:
  131.             pass
  132.         self.failUnless(result[1], 'Argument passing for thread creation using kwargs failed')
  133.         _thread.start_new_thread(arg_tester, (testing_queue, True), {
  134.             'arg2': True })
  135.         result = testing_queue.get()
  136.         if result[0]:
  137.             pass
  138.         self.failUnless(result[1], 'Argument passing for thread creation using both tuple and kwargs failed')
  139.  
  140.     
  141.     def test_multi_creation(self):
  142.         
  143.         def queue_mark(queue, delay):
  144.             '''Wait for ``delay`` seconds and then put something into ``queue``'''
  145.             time.sleep(delay)
  146.             queue.put(_thread.get_ident())
  147.  
  148.         thread_count = 5
  149.         testing_queue = Queue.Queue(thread_count)
  150.         if test_support.verbose:
  151.             print 
  152.             print '*** Testing multiple thread creation (will take approx. %s to %s sec.) ***' % (DELAY, thread_count)
  153.         
  154.         for count in xrange(thread_count):
  155.             if DELAY:
  156.                 local_delay = round(random.random(), 1)
  157.             else:
  158.                 local_delay = 0
  159.             _thread.start_new_thread(queue_mark, (testing_queue, local_delay))
  160.         
  161.         time.sleep(DELAY)
  162.         if test_support.verbose:
  163.             print 'done'
  164.         
  165.         self.failUnless(testing_queue.qsize() == thread_count, 'Not all %s threads executed properly after %s sec.' % (thread_count, DELAY))
  166.  
  167.  
  168.  
  169. def test_main(imported_module = None):
  170.     global _thread, DELAY
  171.     if imported_module:
  172.         _thread = imported_module
  173.         DELAY = 2
  174.     
  175.     if test_support.verbose:
  176.         print 
  177.         print '*** Using %s as _thread module ***' % _thread
  178.     
  179.     test_support.run_unittest(LockTests, MiscTests, ThreadTests)
  180.  
  181. if __name__ == '__main__':
  182.     test_main()
  183.  
  184.